<template>
  <div class="recharge">
    <div class="container">
      <div class="header">
        <div class="dateSelect">
          <el-date-picker v-model="dateValue"
                          type="daterange"
                          range-separator="~"
                          :default-time="[,'23:59:59']"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          @change="getDate">
          </el-date-picker>
        </div>
        <div>
          <el-select v-model="customerValue"
                     placeholder="请选择客户"
                     class="mr20"
                     @change="choiceCustomer"
                     clearable>
            <el-option v-for="item in customerList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-select v-model="productValue"
                     placeholder="请选择产品"
                     class="mr20"
                     @change="choiceProduct"
                     clearable>
            <el-option v-for="item in productList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
          <el-input class="ipt"
                    v-model="input"
                    placeholder="请输入设备信息"></el-input>
          <el-button type="primary"
                     @click="search"
                     icon="el-icon-search">查询</el-button>

          <div class="result">
            <p>一共<span class="result_num">{{totals}}</span>项</p>
            <p><span>总计:</span><span class="result_text">{{price}}￥</span></p>
          </div>
        </div>
      </div>
      <div class="main">
        <gl-table :columns='list'
                  :data="lists"
                  class="gl-table"
                  @button="changeButton">
        </gl-table>
        <gl-pagination :total='totals'
                       class="page"
                       :curr="curr"
                       :size="limits"
                       @callback='changePage'></gl-pagination>
      </div>
      <!--详情弹窗-->
      <el-dialog :visible.sync="orderDio"
                 class="w75 mlr-auto"
                 @open='openDialog'>
        <span slot="title"
              style="fontSize:14px;">充值订单详情</span>
        <div class="detailInfo">
          <div class="left">
            <p v-for="(item,index) in detailInfoList.left"
               :key="index">
              <span>{{item.label}}</span>
              <span>{{item.value}}</span>
            </p>
          </div>
          <div class="right">
            <p v-for="(item,index) in detailInfoList.right"
               :key="index">
              <span>{{item.label}}</span>
              <span>{{item.value}}</span>
            </p>
          </div>
        </div>
        <span slot="footer">
          <el-button @click="orderDio = false">取 消</el-button>
          <el-button type="primary"
                     @click="orderDio = false">确 定</el-button>
        </span>
      </el-dialog>
      <gldelbtn :show="isShow"
                :text='text'
                @cancel="canceled"
                @determine='defineCallback'></gldelbtn>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'Recharge',
  components: {},
  data() {
    return {
      productList: [{ name: '全部', id: '0' }],
      customerList: [{ name: '全部', id: '0' }],
      customerValue: '',
      productValue: '',
      dateValue: '',
      listParams: {
        start_time: '',
        end_time: ''
      },
      input: '',
      lists: [],
      list: [
        { label: '订单ID', prop: 'id' },
        { label: '所属客户', prop: 'customer_name' },
        { label: '所属产品', prop: 'product_name' },
        { label: '充值用户', prop: 'phone' },
        { label: '增加时长', prop: 'use_times' },
        { label: '充值金额', prop: 'real_pay' },
        { label: '充值时间', prop: 'update_time' },
        { label: '操作', prop: '', btns: ['详情'] }
      ],
      orderDio: false, // 订单弹窗
      totals: 0,
      curr: 1,
      limits: 10,
      order_id: '',
      price: '0.00',
      detailInfoList: {
        left: [],
        right: []
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  watch: {
    input() {
      if (!this.input) {
        this.getRechargeList()
      }
    }
  },
  methods: {
    // 充值单列表
    getRechargeList() {
      let params = {
        company_id: this.userInfo.company_id,
        curr: this.curr,
        limits: this.limits,
        customer_id: this.customerValue,
        product_id: this.productValue,
        start_time: this.listParams.start_time,
        end_time: this.listParams.end_time,
        keyword: this.input
      }
      this.http
        .$post('admin/order.api/get_new_recharge_list', params, {
          loading: true
        })
        .then(res => {
          if (res) {
            this.totals = res.total
            this.price = res.price
            if (res.list) {
              let arr = res.list
              arr.map(val => {
                val.use_times = val.use_times + '分钟'
                val.customer_name =
                  val.customer_name == null ? '无' : val.customer_name
                val.product_name =
                  val.product_name == null ? '无' : val.product_name
              })
              this.lists = arr
            } else {
              this.lists = []
              this.totals = 0
              this.price = '0.00'
            }
          }
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    },
    // 分页
    changePage(val) {
      this.curr = val.curr
      this.limits = val.size
      this.getRechargeList()
    },

    // 操作
    changeButton(val) {
      this.orderDio = true
      this.detailInfoList = {
        left: [
          { label: '订单编号:', value: val.row.out_trade_no },
          { label: '充值时间:', value: val.row.update_time },
          { label: '支付方式:', value: val.row.pay_name },
          { label: '充值金额:', value: val.row.real_pay + '元' },
          { label: '套餐名称:', value: val.row.name },
          { label: '增加使用时长:', value: val.row.use_times }
        ],
        right: [
          { label: '用户:', value: val.row.phone },
          { label: '设备ID:', value: val.row.robot_id },
          { label: '设备识别号:', value: val.row.device_code },
          { label: '所属客户:', value: val.row.customer_name },
          { label: '所属产品:', value: val.row.product_name },
          { label: '所属应用:', value: val.row.app_name }
        ]
      }
    },
    // 确定
    addOrder() {},

    // 客户列表
    getCustomer() {
      this.http
        .$post('admin/customer.api/list_customers', {
          company_id: this.userInfo.company_id
        })
        .then(res => {
          res.map(val => {
            this.customerList.push(val)
          })
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    },
    // 产品列表
    getProduct() {
      this.http
        .$post('admin2/product.api/product_lists', {
          company_id: this.userInfo.company_id
        })
        .then(res => {
          res.list.map(val => {
            this.productList.push(val)
          })
        })
        .catch(err => {
          this.$message.error(err.msg)
        })
    },
    // 日期
    getDate() {
      let { listParams, dateValue } = this
      ;[listParams.start_time, listParams.end_time] = dateValue || []
      listParams.start_time /= 1000
      listParams.end_time /= 1000
      this.curr = 1
      this.getRechargeList()
    },
    // 选择客户
    choiceCustomer() {
      this.curr = 1

      this.getRechargeList()
    },
    // 选择产品
    choiceProduct() {
      this.curr = 1
      this.getRechargeList()
    },
    // 搜索
    search() {
      this.curr = 1
      this.getRechargeList()
    },
    // 选择器
    formSelectChange(val) {
      if (val.prop == 'customerName') {
        this.getContract()
      }
    }
  },
  created() {
    this.getRechargeList()
    this.getCustomer()
    this.getProduct()
  },
  mounted() {}
}
</script>
<style lang='less' scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid rgba(235, 235, 235, 1);
}

.recharge {
  margin: 0 auto;

  .container {
    background: #fff;
    padding: 30px 20px;
    .header {
      .dateSelect {
        padding-bottom: 20px;
      }

      .ipt {
        width: 20%;
        padding-right: 20px;
      }
      .result {
        // width: 1084px;
        height: 32px;
        line-height: 32px;
        background-color: rgba(230, 247, 255, 1);
        border: 1px solid rgba(145, 213, 255, 1);
        margin: 20px 0;
        color: #666;
        font-size: 12px;
        p {
          display: inline-block;
          padding-left: 20px;
          vertical-align: top;
          .result_num {
            color: #1890ff;
            font-weight: 400;
            padding: 0 5px;
          }
          .result_text {
            // display: inline-block;
            font-size: 14px;
            color: #666;
            font-weight: 700;
            padding: 0 8px;
          }
        }
      }
    }
  }
  .main {
    .gl-table {
      text-align: center;
    }
  }
}

.orderform {
  padding: 0 30px;
}
.detailInfo {
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.647058823529412);
  text-align: left;
  p {
    line-height: 60px;
    white-space: nowrap;
    overflow: hidden;
  }
  span {
    padding-left: 15px;
  }
  .left {
    // width: 280px;
    padding-left: 25px;
    display: inline-block;
  }
  .right {
    // width: 300px;
    padding-left: 20px;
    display: inline-block;
  }
}
</style>